﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Landscape.Business.Entities;
using NHibernate.Criterion;

namespace Landscape.Business.FNH.Activities
{
    public class MaintenanceActivities : BusinessActivities
    {
        public MaintenanceActivities()
            : base()
        { }

        public MaintenanceActivities(ISession pSession)
            : base(pSession)
        { }
       

        #region Vessel Jobs Related Methods

        public Job GetJobByCode(Guid pJobCode)
        {
            return CurrentSession.Get<Job>(pJobCode);
        }

        #endregion

        public IList<Job> GetJobsByVesselItem(Guid pVesselItemCode, int pPageNo, int pPageSize)
        {
            return CurrentSession.Get<VesselMachine>(pVesselItemCode).Jobs.Where(Exp => !Exp.IsDeleted).Skip(pPageNo * pPageSize).Take(pPageSize).ToList<Job>();
        }

        public IList<VesselMachine> GetVesselSystems(Guid pVesselCode, Guid pParentCode)
        {
            if (pParentCode == null || pParentCode == Guid.Empty)
            {
                return CurrentSession.Get<Vessel>(pVesselCode).Items.Where(Exp => Exp.Type.Parent == null).ToList();
            }
            else
            {
                var parentVesselMachine = CurrentSession.Get<VesselMachine>(pParentCode);
                return CurrentSession.Get<Vessel>(pVesselCode).Items.Where(Exp => Exp.Type.Parent != null && Exp.Type.Parent.ID == parentVesselMachine.Type.ID).ToList();
            }
        }

        public IList<VesselMachine> GetVesselSystems(Guid pVesselCode)
        {
            return CurrentSession.Get<Vessel>(pVesselCode).Items.Where(Exp => Exp.IsDeleted != true).ToList();
        }

        public VesselMachine GetVesselItemByCode(Guid pVesselItemCode)
        {
            return CurrentSession.Get<VesselMachine>(pVesselItemCode);
        }

        public void AddMachineItemToVessel(Guid pItemCode, Guid pVesselCode, Guid pParentVesselItemCode, Guid pUserCode)
        {
            var vesselObject = CurrentSession.Get<Vessel>(pVesselCode);
            var machienItem = CurrentSession.Get<TemplateMachinery>(pItemCode).MachineItem;            

            VesselMachine vi = new VesselMachine();
            vi.CreatedBy = pUserCode;
            vi.CreatedDate = DateTime.Now;
            vi.Status = MachineStatus.NOT_IN_USE;
            vi.IsDeleted = false;
            vi.IsDisabled = false;
            vi.UpdatedBy = pUserCode;
            vi.UpdatedDate = DateTime.Now;            

            vesselObject.Items.Add(vi);

            CurrentSession.SaveOrUpdate(vesselObject);
        }
    }
}
